﻿Function Get-2003MailboxStatistics {
  Param(
     [String]$Identity = "",
     [String]$Server = $Env:ComputerName,
     [String]$Database = ""
  )

  $Filter = "ServerName='$Server'"
  if ($Database -ne "" ) {
    $Filter = "$Filter AND StoreName='$Database'"
  }
  if ($Identity -ne "") {
    $Filter = "$Filter AND (MailboxGuid='{$Identity}' OR LegacyDN='$Identity'"
    $Filter = "$Filter OR MailboxDisplayName='$Identity')"
  }

  Get-WMIObject -ComputerName $Server `
    -Namespace "root/MicrosoftExchangeV2" -Class "Exchange_Mailbox" `
    -Filter $Filter | `
  Select-Object `
    AssocContentCount, `
    @{n='DateDiscoveredAbsentInDs';e={
      If ($_.DateDiscoveredAbsentInDs -ne $Null) {
        [Management.ManagementDateTimeConverter]::ToDateTime(`
          $_.DateDiscoveredAbsentInDs)
      }} }, `
    MailboxDisplayName, TotalItems, LastLoggedOnUserAccount, `
    @{n='LastLogonTime';e={ if ($_.LastLogonTime -ne $Null) { `
      [Management.ManagementDateTimeConverter]::ToDateTime($_.LastLogonTime)}}
    }, `
    @{n='LastLogoffTime';e={ if ($_.LastLogoffTime -ne $Null) { `
      [Management.ManagementDateTimeConverter]::ToDateTime($_.LastLogoffTime)}}
    }, `
    LegacyDN, `
    @{n='MailboxGuid';e={
      ([String]$_.MailboxGuid).ToLower() -Replace "\{|\}" }}, `
    @{n='ObjectClass';e={ "Mailbox" }}, `
    @{n='StorageLimitStatus';e={ `
      Switch ($_.StorageLimitInfo) {
        1 { "BelowLimit" }
        2 { "IssueWarning" }
        4 { "ProhibitSend" }
        8 { "NoChecking" }
        16 { "MailboxDisabled" } }} }, `
    DeletedMessageSize, Size, `
    @{n='Database';e={
      "$($_.ServerName)\$($_.StorageGroupName)\$($_.StoreName)" }}, `
    ServerName, StorageGroupName, StoreName, `
    @{n='Identity';e={ ([String]$_.MailboxGuid).ToLower() -Replace "\{|\}" }}
}

